package xin.lhuo.we.server.dao;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;
import xin.lhuo.we.server.entity.Node;

import java.util.Date;
import java.util.List;

/**
 * @author Lhuo
 * @date 2019/6/29 17:30
 */
@Mapper
@Component
public interface NodeMapper {
    @Insert("insert into we_node (user_id,title,category,content,content_html,image_key,author,create_time) " +
            "values (#{userId},#{title},#{category},#{content},#{contentHtml},#{imageKey},#{author},#{createTime})")
    void saveNode(Node node);

    @Select("select id,title,category,image_key,author,create_time from we_node where user_id = #{userId}")
    @Results(id = "node",
            value = {
                    @Result(column = "id", property = "id", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
                    @Result(column = "user_id", property = "userId", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
                    @Result(column = "title", property = "title", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "category", property = "category", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "content", property = "content", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "content_html", property = "contentHtml", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "image_key", property = "imageKey", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "author", property = "author", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "create_time", property = "createTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP)
            })
    List<Node> getNode(Integer userId);

    @Select("select * from we_node where id = #{id}")
    @ResultMap("node")
    Node getNodeById(Integer id);

    @Delete("delete from we_node where id = #{id}")
    void deleteNode(Integer id);
}
